<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
	</head>

	<body style="padding:0px;background-color: white">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div>
			<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
			<form id="ff" method="post">
				<table id="basicInfo" cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0px auto;">
					<tr>
						<td width="15%">
							<h id="star">* </h>事件编号</td>
						<td width="30%">
							<input class="easyui-textbox" style="width: 80%" type="text" id="input_ORDER_NO" data-options="prompt:'由系统自动生成，不需填写',disabled:true" />
						</td>
						<td width="15%">
							<h id="star">* </h>事件发生时间</td>
						<td width="30%">
							<input class="easyui-datebox" style="width: 80%" type="text" name="TIME" id="input_TIME" data-options='required:true' />
						</td>
					</tr>
					<tr>
						<td>
							<h id="star">* </h>事件描述</td>
						<td>
							<input class="easyui-textbox" style="width: 80%" type="text" name="SECURITYDES" id="input_SECURITYDES" data-options='required:true' />
						</td>
						<td>
							<h id="star">* </h>是否需要整改</td>
						<td>
							<input class="easyui-combobox" style="width: 80%" type="text" name="ISRECTIFICATION" id="input_ISRECTIFICATION" data-options='required:true' />
						</td>
					</tr>
				</table>
				<table>
					<tr id="nodisplay" style="display: none">
						<td>ID</td>
						<td>
							<input class="easyui-textbox" type="text" id="input_ID" />
						</td>
					</tr>
				</table>
				<div class="easyui-tabs" id="div_tabs" style="width: 100%;">
					<div title="安全事件" style="overflow: hidden">
						<table id="tt" style="margin: 0px auto;width: 100%;">
						</table>
					</div>
					<div title="整改事件" style="overflow: hidden">
						<table id="dg" style="margin: 0px auto;width: 100%;">
						</table>
					</div>
				</div>
				<div style="margin-left: 20%;" id="new_footer">

				</div>
			</form>
		</div>
	</body>
	<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../JS/common.js"></script>
	<script type="text/javascript" src="../../../JS/finance.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
	<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
	<script type="text/javascript" src="../../../JS/MarketShareFunction/DialogShareFunction.js"></script>
	<script type="text/javascript">
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var isVerify = 0;
		var rowData1 = new Array(); //用于记录删除安全事件明细的数组
		var rowData2 = new Array(); //用于记录删除整改事件明细的数组
		var Delrow1 = 0; //用于计数删除多少条安全事件明细
		var Delrow2 = 0; //用于计数删除多少条整改事件明细
		var currRow;
		var _detailToolbarDataOne;
		var _detailToolbarDataTwo;
		_detailToolbarDataOne = AddDetailToolBar(7);
		_detailToolbarDataTwo = AddDetailToolBarTwo(7);

		function AddDetailToolBarTwo(type) {
			var Data = [{
				text: '新增明细',
				iconCls: 'icon-add',
				handler: function() {
					$('#dg').edatagrid('addRow');
				}
			}, {
				text: '取消',
				iconCls: 'icon-cancel',
				handler: function() {
					$('#dg').edatagrid('cancelRow');
				}
			}, {
				text: '删除明细',
				iconCls: 'icon-remove',
				handler: function() {
					$('#dg').edatagrid('destroyRow');
				}
			}];
			var data = [];
			var s;
			for(var i = 0; i < Data.length; i++) {
				s = type & (1 << i);
				if(s != 0) {
					data.push(Data[i]);
				}
			}
			return data;
		}
		$(document).ready(function(){
		if(id != AddFlag && id != undefined) {
			currid = id;
			loadRemote();
		}
		})
		getPageMaxAndMin();
		addFooter($("#new_footer"));

		var receipts_type = RECEIPT_FRONT_SYMBOL;
		receipts_type += RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_ADD_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_DELETE_SYMBOL;
		receipts_type += RECEIPT_VERIFY_SYMBOL;
		receipts_type += RECEIPT_UNVERIFY_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_EXPORT_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);

		GetDetailTableOne();
		$('#tt').datagrid('hideColumn', 'ID');
		$('#tt').datagrid('hideColumn', 'ORDER_ID');
		$('#tt').datagrid('hideColumn', 'TENANT_ID');

		GetDetailTableTwo();
		$('#dg').datagrid('hideColumn', 'ID');
		$('#dg').datagrid('hideColumn', 'ORDER_ID');

		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/market/security/getmaxminid.do";
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
						console.log("Maxpage" + MaxPage);
						console.log("MinPage" + MinPage);
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		$('#input_ISRECTIFICATION').combobox({
			valueField: 'ID',
			textField: 'NAME',
			panelHeight: 'auto',
			data: [{
				'ID': '0',
				'NAME': '不需要',
				'selected': 'true'
			}, {
				'ID': '1',
				'NAME': '需要'
			}]
		});

		//导出
		function exportExcel() {
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						data: {
							"ID": currid
						},
						url: RemoteLinkAddress + "/market/security/excelorder.do",
						dataType: "json", //服务器返回的数据类型
						success: function(result) {
							window.location.href = result.path;
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}

		function frontpage() { //上一张
			if(currPage > MinPage) {
				var _url = RemoteLinkAddress + '/market/security/pre.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() { //下一张
			if(currPage < MaxPage) {
				var _url = RemoteLinkAddress + '/market/security/next.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert(" 提示", "没有下张");
			}
		}

		function newdata() {
			$("#notreview").empty(); //去掉审核或者未审核的图片
			isVerify = 0; //将状态改为未审核
			currid = AddFlag;
			currPage = 0;
			resetData();
			$('#tt').datagrid('loadData', {
				total: 0,
				rows: []
			});
			$('#dg').datagrid('loadData', {
				total: 0,
				rows: []
			});
		}

		function save() {
			if(isVerify === "1") {
				$.messager.alert("提示", "该单据已审核，不能修改！");
				return;
			}
			var msg = checkDataFormat();
			if(msg != "验证成功") {
				$.messager.alert("警告", msg);
				return;
			}
			var isValid = isTrueData();
			if(isValid != "验证成功") {
				$.messager.alert("警告", isValid);
				return;
			}
			var s = $('#tt').edatagrid('getRows');
			if(s.length == 0) {
				$.messager.alert("警告", "明细不能为空");
				return;
			}
			if(currid != AddFlag) {
				UpdateData();
			} else {
				SaveData();
			}
		}

		function deletedata() { //删除
			if(isVerify === "1") {
				$.messager.alert("提示", "该单据已审核，不能删除！");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("提示", "该页面为新增页面，不能进行删除操作！");
				return;
			}
			$.messager.confirm('Confirm', '是否删除', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/market/security/delete.do", {
							ID: currid
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({ // show success message
									title: '成功',
									msg: '删除成功'
								});
								nextpage();
							} else {
								$.messager.alert({ // show error message
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function verifydata() { //审核单据
			if(currid == AddFlag) {
				$.messager.alert("提示", "该页面为新增页面，不能进行审核操作！");
				return;
			}
			if(isVerify == 1) {
				$.messager.alert("警告", "已经审核无须再审");
				return;
			}
			var _url = RemoteLinkAddress + "/market/security/verify.do";
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: {
					ID: currid
				},
				type: "POST",
				success: function(result) {
					$.messager.alert("成功", "审核成功");
					isVerify = "1";
					verifyStatus(isVerify, true);
				},
				error: function(result) {
					$.messager.alert("失败", "审核失败");
				}
			});
		}

		function unverifydata() { //弃审单据
			if(currid == AddFlag) {
				$.messager.alert("提示", "该页面为新增页面，不能进行弃审操作！");
				return;
			}
			if(isVerify == "0") {
				$.messager.alert("警告", "已经弃审无须再弃审");
				return;
			}
			$.messager.confirm("警告", "你确认要弃审该条单据吗", function(r) {
				if(r) {
					var _url = RemoteLinkAddress + "/market/security/unverify.do";
					$.ajax({
						dataType: 'JSON',
						url: _url,
						data: {
							ID: currid,
						},
						type: "POST",
						success: function(result) {
							$.messager.alert("成功", "弃审成功");
							isVerify = "0";
							verifyStatus(isVerify, true);
						},
						error: function(result) {
							$.messager.alert("失败", "弃审失败");
						}
					});
				}
			});
		}

		function searchmore() { //查询
			AddTabIn("食品安全事件查询", parent.$('#tabs'), "UI/Market/FoodSecurityManagement/FoodSecurityManagementSearch.html");
		}

		function print() { //打印
			var pLocation = "";
			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不可打印！");
				return;
			}
			if(isVerify == "0") {
				$.messager.alert("警告", "单据未审核，不能打印！");
				return;
			}
			pLocation = "FoodSecurityManagementPrint.html?id=" + currid;
			window.open(pLocation);
		}

		function loadRemote() {
			var _url = RemoteLinkAddress + '/market/security/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		function SaveData() {
			var data = Object();
			data = submitFormData();
			data.rows_man = new Array();
			var dataRowsOne = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataRowsOne.length; i++) {
				var _index = i;
				data.rows_man[_index] = new Object();
				data.rows_man[_index].ORDER_ID = dataRowsOne[i].ORDER_ID;
				data.rows_man[_index].TENANT_ID = dataRowsOne[i].TENANT_ID;
			}
			data.rows_manage = new Array();
			var dataRowsTwo = $("#dg").datagrid("getRows");
			for(var i = 0; i < dataRowsTwo.length; i++) {
				var _index = i;
				data.rows_manage[_index] = new Object();
				data.rows_manage[_index].ORDER_ID = dataRowsTwo[i].ORDER_ID;
				data.rows_manage[_index].RECTIFICATION_TIME = dataRowsTwo[i].RECTIFICATION_TIME;
				data.rows_manage[_index].DESCRIPTION = dataRowsTwo[i].DESCRIPTION;
				data.rows_manage[_index].AGENT = dataRowsTwo[i].AGENT;
				data.rows_manage[_index].MEMO = dataRowsTwo[i].MEMO;
			}
			submitData(data);
			console.log(JSON.stringify(data));
		}

		function UpdateData() {
			var data = submitFormData();
			var dataGridRow1 = $('#tt').datagrid('getRows');
			var _data1 = $.merge(dataGridRow1, rowData1); //将删除的明细和剩下的明细一起合并
			data.rows_man = _data1;

			var dataGridRow2 = $('#dg').datagrid('getRows');
			var _data2 = $.merge(dataGridRow2, rowData2); //将删除的明细和剩下的明细一起合并
			data.rows_manage = _data2;
			submitData(data);
		}

		function submitData(_data) {
			var _url = "";
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/market/security/save.do";
			} else {
				_url = RemoteLinkAddress + "/market/security/update.do";
			}
			var _senddata = JSON.stringify(_data);
			console.log(_senddata);
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					var revData = result.msg.split(";");
					if(revData[0] != "success"){
						$.messager.alert("错误","保存失败")
					}
					if(currid == AddFlag) {
						$("#input_ORDER_NO").textbox("setValue", revData[2]);
						$("#input_ID").textbox("setValue", revData[1]);
						currid = revData[1];
						currPage = revData[1];
						$.messager.alert("提示", "新建保存成功");
						$(document).ready(function(){
							loadRemote();
						});
						return;
					}
					$.messager.alert("提示", "修改保存成功");
				},
				error: function(result) {
					$.messager.alert("错误", "保存不成功");
				}
			});
		}

		function GetDetailTableOne() {
			$('#tt').edatagrid({
				height: 330,
				toolbar: _detailToolbarDataOne,
				collapsible: 'true', //是否折叠：是
				singleSelect: 'true', //是否单选：是
				rownumbers: 'true',
				fitColumns: 'true', //自动调整列：是
				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center'
					}, {
						field: 'ORDER_ID',
						title: '事件ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "ORDER_ID",
								textField: "ORDER_ID",
								editable: false
							}
						}
					}, {
						field: 'TENANT_ID',
						title: '租户ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "TENANT_ID",
								textField: "TENANT_ID",
								editable: false
							}
						}
					}, {
						field: 'NAME',
						title: '租户姓名',
						width: '18%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "NAME",
								textField: "NAME",
								editable: true,
								panelHeight: '150',
								mode: 'remote',
								method: 'get',
								url: RemoteLinkAddress + '/market/tenant/listall.do',
								onSelect: function(record) {
									console.log(record);
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'TENANT_ID'
									}).target;
									target.textbox('clear');
									if(record.ID != undefined) {
										target.textbox('setValue', record.ID);
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'TYPE'
									}).target;
									target.textbox('clear');
									if(record.TYPE != undefined) {
										target.textbox('setValue', record.TYPE);
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'GENDER'
									}).target;
									target.textbox('clear');
									if(record.GENDER != undefined) {
										target.textbox('setValue', record.GENDER);
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'IDCARD'
									}).target;
									target.textbox('clear');
									if(record.IDCARD != undefined) {
										target.textbox('setValue', record.IDCARD);
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'TELEPHONE'
									}).target;
									target.textbox('clear');
									if(record.TELEPHONE != undefined) {
										target.textbox('setValue', record.TELEPHONE);
									}
								}
							}
						}
					}, {
						field: 'TYPE',
						title: '类型',
						width: '19%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "TYPE",
								textField: "TYPE",
								editable: true
							}
						},
						formatter: function(value, row, index) {
							if(value == 0) {
								return "租户";
							} else {
								return "小商户";
							}
						}
					}, {
						field: 'GENDER',
						title: '性别',
						width: '19%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "GENDER",
								textField: "GENDER",
								editable: true
							}
						}
					}, {
						field: 'IDCARD',
						title: '身份证',
						width: '21%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "IDCARD",
								textField: "IDCARD",
								editable: true
							}
						}
					}, {
						field: 'TELEPHONE',
						title: '联系电话',
						width: '19%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "TELEPHONE",
								textField: "TELEPHONE",
								editable: true
							}
						}
					}]
				],
				onEndEdit: function(index, row, changes) {
					var idcard = row.IDCARD;
					var num = row.TELEPHONE;
					var tip = checkPhoneNumber(num);
					if(tip != '验证成功') {
						$.messager.alert('警示', tip)
					}
					var str = IdentityCodeValid(idcard);
					if(str != '验证成功') {
						$.messager.alert('警示', str)
					}
					row.ORDER_ID = $('#input_ID').textbox('getValue');
				},
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
					row.ORDER_ID = $('#input_ID').textbox('getValue'); //新增明细时将页面的ID赋给明细的ORDER_ID,并将明细里的ID置为null
					row.ID = null;
				},
				onDestroy: function(index, row) {
					console.log(row);
					row.ORDER_ID = null; //删除明细时将明细的ORDER_ID置为null，并将每条删除的明细保存到数组rowData1
					rowData1[Delrow1] = new Object();
					rowData1[Delrow1] = row;
					Delrow1++;
					console.log(rowData1);
				}
			});
		}

		function GetDetailTableTwo() {
			$('#dg').edatagrid({
				height: 330,
				toolbar: _detailToolbarDataTwo,
				collapsible: 'true', //是否折叠：是
				singleSelect: 'true', //是否单选：是
				rownumbers: 'true',
				fitColumns: 'true', //自动调整列：是
				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center'
					}, {
						field: 'ORDER_ID',
						title: '表单ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "ORDER_ID",
								textField: "ORDER_ID",
								editable: false
							}
						}
					}, {
						field: 'RECTIFICATION_TIME',
						title: '整改时间',
						width: '14%',
						align: 'center',
						editor: {
							type: 'datebox',
							options: {
								valueField: "RECTIFICATION_TIME",
								textField: "RECTIFICATION_TIME",
								editable: true
							}
						}
					}, {
						field: 'DESCRIPTION',
						title: '整改事件说明',
						width: '55%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "DESCRIPTION",
								textField: "DESCRIPTION",
								editable: true
							}
						}
					}, {
						field: 'AGENT',
						title: '经办人',
						width: '13%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "AGENT",
								textField: "AGENT",
								editable: true
							}
						}
					}, {
						field: 'MEMO',
						title: '备注',
						width: '14%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "MEMO",
								textField: "MEMO",
								editable: true
							}
						}
					}]
				],
				onEndEdit: function(index, row, changes) {
					row.ORDER_ID = $('#input_ID').textbox('getValue');
				},
				onAdd: function(index, row) {
					row.ORDER_ID = $('#input_ID').textbox('getValue'); //新增明细时将页面的ID赋给明细的ORDER_ID,并将明细里的ID置为null
					row.ID = null;
				},
				onDestroy: function(index, row) {
					console.log(row);
					row.ORDER_ID = null; //删除明细时将明细的ORDER_ID置为null，并将每条删除的明细保存到数组rowData2
					rowData2[Delrow2] = new Object();
					rowData2[Delrow2] = row;
					Delrow2++;
					console.log(rowData2);
				}
			});
		}

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						console.log(result);
						isVerify = result.ISVERIFY;
						verifyStatus(isVerify, true);
						getFormData(result);
						currPage = result.ID;
						currid = result.ID;
						var SecMan = result.rows_man;
						var SecManage = result.rows_manage;
						$('#tt').edatagrid('loadData', SecMan);
						$('#dg').edatagrid('loadData', SecManage);
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function isTrueData() {
			var tip = "验证成功";
			var _value;
			var _name = "";
			if($("#basicInfo") == undefined) {
				console.log("检查basicInfo是否存在");
				return;
			}
			$("#basicInfo input[name]").each(function(index) {
				_value = $(this).val().trim();
				_name = $(this).attr("name");
				if(_value.length == 0 && _name === "ISRECTIFICATION") {
					console.log(_name + ":" + _value);
					tip = "请将数据填写完整";
					return;
				}
			});
			return tip;
		}
	</script>

</html>